package org.ww.seckill.scheduled;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.ww.seckill.service.SeckillService;

import java.util.concurrent.TimeUnit;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-11-05 14:32
 **/

@Service
public class SeckillSkuScheduled {

    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;

    private static final String SECKILL_UPLOAD_LOCK = "seckill:upload:lock";

    //TODO:幂等性处理
    @Scheduled(cron = "*/3 * * * * ?")
    public void uploadSeckillLatested3Days(){
        System.out.println("秒杀商品上架-----");
        //分布式锁：锁的业务执行完成后，状态已经更新完成，其他锁拿到的状态就是最新的状态
        RLock lock = redissonClient.getLock(SECKILL_UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try{
            seckillService.uploadSeckillLatested3Days();
        }finally {
            lock.unlock();
        }

    }

}
